<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Figure 8.8: Simplest linear discrimination</title>
<link rel="canonical" href="http://cvxr.com/cvx/examples/cvxbook/Ch08_geometric_probs/html/linear_discr.html">
<link rel="stylesheet" href="../../../examples.css" type="text/css">
</head>
<body>
<div id="header">
<h1>Figure 8.8: Simplest linear discrimination</h1>
Jump to:&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#source">Source code</a>&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#output">Text output</a>
&nbsp;&nbsp;&nbsp;&nbsp;
<a href="#plots">Plots</a>
&nbsp;&nbsp;&nbsp;&nbsp;<a href="../../../index.html">Library index</a>
</div>
<div id="content">
<a id="source"></a>
<pre class="codeinput">
<span class="comment">% Boyd &amp; Vandenberghe "Convex Optimization"</span>
<span class="comment">% Joelle Skaf - 10/16/05</span>
<span class="comment">% (a figure is generated)</span>
<span class="comment">%</span>
<span class="comment">% The goal is to find a function f(x) = a'*x - b that classifies the points</span>
<span class="comment">% {x_1,...,x_N} and {y_1,...,y_M}. a and b can be obtained by solving a</span>
<span class="comment">% feasibility problem:</span>
<span class="comment">%           minimize    0</span>
<span class="comment">%               s.t.    a'*x_i - b &gt;=  1     for i = 1,...,N</span>
<span class="comment">%                       a'*y_i - b &lt;= -1     for i = 1,...,M</span>

<span class="comment">% data generation</span>
n = 2;
randn(<span class="string">'state'</span>,3);
N = 10; M = 6;
Y = [1.5+1*randn(1,M); 2*randn(1,M)];
X = [-1.5+1*randn(1,N); 2*randn(1,N)];
T = [-1 1; 1 1];
Y = T*Y;  X = T*X;

<span class="comment">% Solution via CVX</span>
fprintf(<span class="string">'Finding a separating hyperplane...'</span>);

cvx_begin
    variables <span class="string">a(n)</span> <span class="string">b(1)</span>
    X'*a - b &gt;= 1;
    Y'*a - b &lt;= -1;
cvx_end

fprintf(1,<span class="string">'Done! \n'</span>);

<span class="comment">% Displaying results</span>
linewidth = 0.5;  <span class="comment">% for the squares and circles</span>
t_min = min([X(1,:),Y(1,:)]);
t_max = max([X(1,:),Y(1,:)]);
t = linspace(t_min-1,t_max+1,100);
p = -a(1)*t/a(2) + b/a(2);

graph = plot(X(1,:),X(2,:), <span class="string">'o'</span>, Y(1,:), Y(2,:), <span class="string">'o'</span>);
set(graph(1),<span class="string">'LineWidth'</span>,linewidth);
set(graph(2),<span class="string">'LineWidth'</span>,linewidth);
set(graph(2),<span class="string">'MarkerFaceColor'</span>,[0 0.5 0]);
hold <span class="string">on</span>;
plot(t,p, <span class="string">'-r'</span>);
axis <span class="string">equal</span>
title(<span class="string">'Simple classification using an affine function'</span>);
<span class="comment">% print -deps lin-discr.eps</span>
</pre>
<a id="output"></a>
<pre class="codeoutput">
Finding a separating hyperplane... 
Calling sedumi: 16 variables, 3 equality constraints
   For improved efficiency, sedumi is solving the dual problem.
------------------------------------------------------------
SeDuMi 1.21 by AdvOL, 2005-2008 and Jos F. Sturm, 1998-2003.
Alg = 2: xz-corrector, Adaptive Step-Differentiation, theta = 0.250, beta = 0.500
eqs m = 3, order n = 17, dim = 17, blocks = 1
nnz(A) = 48 + 0, nnz(ADA) = 9, nnz(L) = 6
 it :     b*y       gap    delta  rate   t/tP*  t/tD*   feas cg cg  prec
  0 :            9.03E+01 0.000
  1 :   0.00E+00 3.20E+01 0.000 0.3538 0.9000 0.9000  -2.42  1  1  3.8E+01
  2 :   0.00E+00 7.37E+00 0.000 0.2305 0.9000 0.9000  -0.26  1  1  1.4E+01
  3 :   0.00E+00 2.38E-01 0.000 0.0323 0.9900 0.9900   0.62  1  1  5.6E-01
  4 :   0.00E+00 8.82E-06 0.000 0.0000 1.0000 1.0000   0.99  1  1  
iter seconds digits       c*x               b*y
  4      0.0   Inf  0.0000000000e+00  0.0000000000e+00
|Ax-b| =   0.0e+00, [Ay-c]_+ =   0.0E+00, |x|=  0.0e+00, |y|=  5.0e+00

Detailed timing (sec)
   Pre          IPM          Post
1.000E-02    3.000E-02    0.000E+00    
Max-norms: ||b||=0, ||c|| = 1,
Cholesky |add|=0, |skip| = 0, ||L.L|| = 1.
------------------------------------------------------------
Status: Solved
Optimal value (cvx_optval): -0
Done! 
</pre>
<a id="plots"></a>
<div id="plotoutput">
<img src="linear_discr__01.png" alt=""> 
</div>
</div>
</body>
</html>